package me.zfz.business.controller.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import me.zfz.base.bean.SmartResponse;
import me.zfz.business.controller.base.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mixsmart.utils.StringUtils;

/**
 * 异常处理控制器
 *
 * @author author
 */
@Controller
@RequestMapping("/exception")
public class ExceptionController extends BaseController {

    private static final String VIEW_DIR = "error/";

    /**
     * 返回JSON格式的错误信息
     *
     * @param msg
     * @return
     */
    @RequestMapping("/json")
    @ResponseBody
    public SmartResponse<String> json(String msg) {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        if (StringUtils.isNotEmpty(msg)) {
            try {
                msg = URLDecoder.decode(msg, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            smartResp.setMsg(msg);
        }
        return smartResp;
    }

    /**
     * 500错误
     *
     * @return
     */
    @RequestMapping("/500")
    public ModelAndView error500() {
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName(VIEW_DIR + "500");
        return modelView;
    }

    /**
     * 404错误
     *
     * @return
     */
    @RequestMapping("/404")
    public ModelAndView error404() {
        ModelAndView modelView = new ModelAndView();
        modelView.setViewName(VIEW_DIR + "404");
        return modelView;
    }

    @RequestMapping("/noaccess")
    @ResponseBody
    public SmartResponse<String> noAccess() {
        SmartResponse<String> smartResp = new SmartResponse<String>();
        smartResp.setMsg("没有访问权限");
        return smartResp;
    }
}
